require(['./config'], () => {
    require(['template','header','footer','cookie'], (template,header) => {
      class gwc {
        constructor () {
        this.renderTable()
        this.setCheckAllstate()
        this.change()
        this.zongJa ()
        this.addBtn()
        this.delete()
        }

        //隐藏页面转换
        renderTable () {
        let cart = localStorage.getItem('cart')
        if (cart) {
        this.cart = JSON.parse(cart)
        $('#shop_cont').html(template('srcShop',{ cart:this.cart }))
        }else {
          $('#shopping_car').hide()
          $('#cart-empty').show()
        }
        }


        //默认勾选
        change () {
          const _this =this
          $('#shop_cont input:checkbox').on('change', function () {
            const id = Number($(this).parents('tr').attr('data-id'))
            _this.cart = _this.cart.map(shop => {
              if (shop.id === id) shop.check = $(this) .prop('checked')
              return shop
            })
            localStorage.setItem('cart', JSON.stringify(_this.cart))
            _this.setCheckAllstate()
            _this.zongJa ()
          })
        }


        //全选
        setCheckAllstate() {
            //当每一个都满足的时候才true，可以判断全选，或者全不选
            //第一个是作为判断
           
            const isAllcheck = this.cart.every(shop => {
              return shop.check
            })
            
            // 判断当全选中时。选中的时候是true，不选中的时候是false
            if (isAllcheck) {
              $('#all-check').prop('checked', true)
              // 有一个不满足的时候就是folse
            } else {
              $('#all-check').prop('checked', false)
            }
            // 如果cart所有数据都是check为true，那么全选checked属性也为true     ????
            //第二条数据作为筛选
            $('#allCheck').prop('checked', this.cart.every(shop => {
              return shop.check
            }))
          
          }


          //总价
          zongJa () {
            const money = this.cart.reduce((all,shop) =>{
              if(shop.check) all += shop.jg * shop.count
              return all
            },0)
            $('#Paymoney').html(money.toFixed(2))
          }


          //删除事件
          delete(){
            const _this = this
            $('#delete').on('click',function(){
              if(confirm('请确认后删除')){
                const id = Number($(this).parent().parent().attr('data-id'))
                console.log(id)

                $(this).parents('#shhp').remove();
                 _this.delete()
                let cart = JSON.parse(localStorage.getItem('cart'))
                _this.cart = cart.filter((shop) => {
                  return shop.id!=id
                })
                console.log(_this.cart)
                localStorage.setItem('cart',JSON.stringify(_this.cart))
                if(_this.cart.length == 0){
                  localStorage.removeItem('cart')
                  $('#cart-empty').show()
                  $('#shopping_car').hide()
                }
              }
              _this.zongJa()
              header.gwcshulian()
            })
          }

          
          //点击增加事件
          addBtn() {
            let sub = $('.minus');
            let add = $('.plus')
            let input = $('.put')
            let value = $('.put').val()
            const _this = this
            var res

            // 增加
            add.on('click',function(){
              let cart = JSON.parse(localStorage.getItem('cart'))
              
              const id = Number($(this).parent().parent().attr("data-id"))
            let input = $(this).prev().val()
            // console.log(input)
              _this.cart = cart.map(item => {
                // console.log(item.id)
                if(item.id == id){
                  res = ++input
                let num =  res*item.jg
                  $(this).parent().nextAll('.xiaoji').html(num)
                  $(this).parents('td').find('.put').val(res)
                  item.count = res
                }
                return item
              })
              localStorage.setItem('cart',JSON.stringify(_this.cart))
              _this.zongJa()
              header.gwcshulian()
            })

            //减少
            sub.on('click',function(){
              let cart = JSON.parse(localStorage.getItem('cart'))
              
              const id = Number($(this).parent().parent().attr("data-id"))
             // console.log($(this).parent().parent().attr("data-id"))
             let input = $(this).next().val()
            console.log(input)
              _this.cart = cart.map(item => {
                // console.log(item.id)
                if(item.id == id){
                  res = --input
                  if(res <1){
                    res = 1
                  }
                let num =  res*item.jg
                  $(this).parent().nextAll('.xiaoji').html(num)
                  $(this).parents('td').find('.put').val(res)
                  item.count = res
                }
                return item
              })
              localStorage.setItem('cart',JSON.stringify(_this.cart))
              _this.zongJa()
              header.gwcshulian()
            })
          }
        
      }
      new gwc()
    })
     
})